<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    barData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose();
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        backgroundColor: '#fff',
        title: {
          text: '任务的完成情况',
          textStyle: {
            fontSize: 19
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#57617B'
            }
          }
        },
        color: '#41a0f9',
        grid: {
          top: 140,
          left: 0,
          right: 0,
          bottom: '2%',
          containLabel: true,
          height: 320
        },
        xAxis: [{
          type: 'category',
          boundaryGap: true,
          axisLine: {
            lineStyle: {
              color: '#f8f8f8'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#9fa0a3',
            textStyle: {
              fontSize: 14
            }
          },
          data: this.barData.timeArray
        }],
        yAxis: [{
          type: 'value',
          // name: '完成比例',
          nameGap: 55,
          nameTextStyle: {
            color: '#55585e',
            fontSize: 18
          },
          min: '0',
          max: '1',
          splitNumber: 5,
          axisTick: {
            show: false
          },
          axisLine: {
            show: false,
            lineStyle: {
              // color: '#57617B'
            }
          },
          axisLabel: {
            margin: 10,
            color: '#7a7a7c',
            textStyle: {
              fontSize: 14
            }
          },
          splitLine: {
            lineStyle: {
              color: '#f8f8f8'
            }
          }
        }],
        series: [
          {
            // name: '任务完成比例',
            type: 'bar',
            smooth: true,
            barWidth: 40,
            data: this.barData.finishTaskNum
          }
        ]
      })
    }
  }
}
</script>
